package com.z.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.z.common.base.Results;
import com.z.admin.dao.UserMapper;
import com.z.admin.dao.UserRoleMapper;
import com.z.admin.entity.User;
import com.z.admin.entity.UserRole;
import com.z.admin.service.IUserService;
import com.z.admin.vo.UserDto;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author z
 * @since 2022-02-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    
    @Override
    public void save(User user, Integer roleId) {
        if(roleId != null){
            userMapper.insert(user);
            userRoleMapper.insert(new UserRole(user.getId(), roleId));
        }
    }

    @Override
    public Results<User> findByPage(Integer offset, Integer limit) {
        return Results.success(userMapper.conutAllUsers(), userMapper.findUserByPage(offset, limit));
    }

    @Override
    public List<User> findUserByPhone(String phone) {
        return userMapper.findUserByPhone(phone);
    }

    @Override
    public User findById(String id) {
        if(StringUtils.isEmpty(id)){
            return new User();
        }
        return userMapper.selectById(Long.parseLong(id));
    }

    @Override
    public void deleteById(String id) {
        userRoleMapper.deleteByUserId(id);
        userMapper.delete(Long.parseLong(id));
    }

    @Override
    public void update(UserDto user, Integer roleId) {
        userMapper.updateById(user);
        UserRole userRole = userRoleMapper.getUserRoleByUserIdAndRoleId(user.getId(), roleId);
        if(null == userRole)
            userRoleMapper.insert(new UserRole(user.getId().intValue(), roleId));
        else
            userRoleMapper.update(new UserRole(userRole.getId(),user.getId(), roleId));
    }

    @Override
    public Results<User> findPageByMap(Map<String, Object> map, Integer offset, Integer limit) {
        StringBuffer where = new StringBuffer("");
        if(map != null && map.size() > 0){
            where.append("where ");
            for(Map.Entry<String, Object> entry : map.entrySet()){
                String[] strArr = entry.getKey().split("_");
                where.append(strArr[0]+" ");
                if(strArr[1].equals("like")){
                    where.append("like '%" +entry.getValue()+"%' and ");
                }else{
                    where.append("= '" +entry.getValue()+"' and ");
                }
            }
            where.delete(where.length()-4, where.length());
        }
        return Results.success(userMapper.conutByMap(where.toString()), userMapper.findPageByWhereSql(where.toString(), offset, limit));
    }

    @Override
    public User findUserByName(String name) {

        return userMapper.selectOne(new QueryWrapper<User>().eq("username", name));
    }
}
